package com.example.comsys.service.impl;

import com.example.comsys.dao.DepartmentDAO;
import com.example.comsys.dataobject.DepartmentDO;
import com.example.comsys.model.Department;
import com.example.comsys.service.DepartmentService;
import com.example.comsys.util.ConvertUtils;
import com.example.comsys.util.ConvertUtils.DepartmentUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * department service impl
 *
 * @author wjh
 * @date 2020/1/2, 周四
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

  @Autowired
  private DepartmentDAO departmentDAO;

  @Override
  public Department getDepartmentById(long id) {
    if (id <= 0) {
      return null;
    }
    DepartmentDO departmentDO = departmentDAO.selectByPrimaryKey(id);
    if (departmentDO == null) {
      return null;
    }
    return ConvertUtils.DepartmentUtils.departmentToModel(departmentDO);
  }

  @Override
  public List<Department> batchQueryDepartments(Long... ids) {
    if (ids == null) {
      return null;
    }
    if (ids.length == 0) {
      return new ArrayList<>();
    }

    List<DepartmentDO> dos = departmentDAO.selectByPrimaryKeys(Arrays.asList(ids));
    if (dos != null && dos.size() > 0) {

      return dos.stream()
          .map(DepartmentUtils::departmentToModel)
          .filter(Objects::nonNull)
          .collect(Collectors.toList());
    }
    return new ArrayList<>();
  }

  @Override
  public List<Department> queryAll() {
    List<DepartmentDO> departmentDos = departmentDAO.selectAll();
    if (CollectionUtils.isEmpty(departmentDos)) {
      return new ArrayList<>();
    } else {
      return departmentDos.stream()
          .map(DepartmentUtils::departmentToModel)
          .filter(Objects::nonNull)
          .collect(Collectors.toList());
    }
  }
}
